


import '@/assets/icon/iconfont';
import {baseURL} from '@/util/util';
import upload from '@/components/upload'

 export default {

		computed: {

		 /* @param {Array | String} data 数据
      * @return {Array} 返回数据
      */
			fileUploadList(){
				return (data)=>
        	 typeof data === 'string' && data.length ? JSON.parse(data) : data
			},

			/* 返回没个文件的图标
			 * @param {String} url 文件路径
			 */
			fileUploadIcon(){
				return (url)=>{
					const postfix = url && url.slice(url.lastIndexOf('.') + 1, url.length);
					let icon = '#icon-'
					if([
					'txt','text','ppt','upload','video','white','zip','word','html','jpg','mp3','pdf','excel','download','ai','gif','png',
					].includes(postfix)) icon += postfix
					else icon += 'white'
					return icon
				}
			}
		},
    methods: { 

      /* 删除单一上传的文件
			 * @param {Object} data 数据
			 * @param {String} url url字段
			 * @param {String} name name字段
       */
      fileUploadDeleteItem(data,url,name){
        if(url) data[url] = ""
        if(name) data[url] = ""
      },

			/* 上传失败 */
			fileUploadError(){
				this.$message.error('上传失败')
			},

      /* 上传一个文件
      * @param {Object} data 数据
      * @param {String} url url字段
      * @param {String} name name字段
       */
      fileUploadChangeItem(info,data,url,name,identifier){
          data[url] = info.url
          data[name] = info.title
					if (this[`uploadSuccess${identifier}`]) {
						this[`uploadSuccess${identifier}`]();
					}
      },
			 /* @param {url} 文件路径
			 @param {fileName} 文件名称
			 @param {isDownload} 文件是否能下载
       */
      downdFile(url,fileName,isDownload){
				if(isDownload){
					const elink = document.createElement('a')
					elink.href = url
					elink.setAttribute('download', fileName)
					elink.style.display = 'none'
					document.body.appendChild(elink)
					setTimeout(() => {
						elink.click()
						document.body.removeChild(elink)
					}, 66)
				}
				
			},
     /* @param {object} info 文件
       * @param {object} dataSource 数据
       * @param {String} fieldId 字段
       */
			fileUploadChange(data, dataSource, fieldId,identifier) {
          const fileList = this.fileUploadList(dataSource[fieldId])
          fileList.push(data)
          dataSource[fieldId] = JSON.stringify(fileList)
					if(this[`uploadSuccess${identifier}`]){
            this[`uploadSuccess${identifier}`]();
          }
			},

			fileUploadItemBefore(file, fileStr, maxLength, fileSize,imageFormat) {
					let fileList = fileStr ? [fileStr] : [];
					return this.fileUploadBefore(file, fileList, maxLength, fileSize,imageFormat)
			},

			/* 上传前执行是否符合数量和文件大小的要求
			 * @param {object} file 文件
			 * @param {number} maxLength 数量最大值
			 * @param {number} fileSize 文件最大值
			 *  @param {number} formatType 文件限制类型
			 *  @param {string} imageFormat 文件限制格式
			 */
			fileUploadBefore(file, fileList, maxLength, fileSize) {
					fileList = this.fileUploadList(fileList)
					if (fileList.length >= maxLength) {
						this.$message.error(`最大上传个数不能超过${maxLength}个`);
						return false
					}
					if (file.size / 1024 / 1024 > fileSize) {
						this.$message.error(`文件大小不能超过${fileSize}MB!`);
						return false
					}
					return true
			},

      /* 删除某一项文件
       * @param {object} fileItem 文件
       * @param {object} dataSource 数据
       * @param {String} fieldId 字段
       */
      fileUploadDeleteList(fileItem,dataSource,fieldId){
        const fileList = this.fileUploadList(dataSource[fieldId])
        const index = fileList?.findIndex(item => item.url === fileItem.url)
        if(index >= 0){
          fileList.splice(index,1)
          dataSource[fieldId] = JSON.stringify(fileList)
        }
      },

    },
		components:{
			upload
		}
  }
